package com.itheima.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.pojo.Permission;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author admin
 */
public interface PermissionMapper extends BaseMapper<Permission> {

    @Select("SELECT DISTINCT\n" +
            "\tp.* \n" +
            "FROM\n" +
            "\ttb_role_permission rp\n" +
            "\tLEFT JOIN tb_permission p ON rp.permission_id = p.id \n" +
            "WHERE\n" +
            "\trp.role_id IN (\n" +
            "\tSELECT\n" +
            "\t\tru.role_id \n" +
            "\tFROM\n" +
            "\t\ttb_role_user ru\n" +
            "\t\tLEFT JOIN tb_user u ON ru.user_id = u.id \n" +
            "\tWHERE\n" +
            "\t\tu.id =(\n" +
            "\t\tSELECT\n" +
            "\t\t\ttb_user.id \n" +
            "\t\tFROM\n" +
            "\t\t\ttb_user \n" +
            "\t\tWHERE\n" +
            "\t\tuser_name = #{userName} \n" +
            "\t))")
    List<Permission> queryPermissionByUserName(@Param("userName") String userName);

    @Select("SELECT DISTINCT\n" +
            "\tp.code \n" +
            "FROM\n" +
            "\ttb_role_permission rp\n" +
            "\tLEFT JOIN tb_permission p ON rp.permission_id = p.id \n" +
            "WHERE\n" +
            "\trp.role_id IN (\n" +
            "\tSELECT\n" +
            "\t\tru.role_id \n" +
            "\tFROM\n" +
            "\t\ttb_role_user ru\n" +
            "\t\tLEFT JOIN tb_user u ON ru.user_id = u.id \n" +
            "\tWHERE\n" +
            "\t\tu.id =(\n" +
            "\t\tSELECT\n" +
            "\t\t\ttb_user.id \n" +
            "\t\tFROM\n" +
            "\t\t\ttb_user \n" +
            "\t\tWHERE\n" +
            "\t\tuser_name = #{userName} \n" +
            "\t))")
    List<String> queryPermissionCodeByUserName(@Param("userName") String userName);

}
